<template>
  <el-container style="height: 600px; border: 1px solid #eee">
    <!-- 展示商品详情的弹出框 -->
    <el-dialog title="商品详情" :visible.sync="showDetailFormVisible">
      <!-- 商品编号 -->
      <el-form :model="showDetailForm">
        <el-form-item label="商品描述" :label-width="formLabelWidth" style="display:inline-block">
          {{ showDetailForm.goods_brief }}
        </el-form-item>
        <br>
        <el-form-item label="商品数量" :label-width="formLabelWidth" style="display:inline-block">
          {{ showDetailForm.goods_num }}
        </el-form-item>
        <br>
        <el-form-item label="商品价格" :label-width="formLabelWidth" style="display:inline-block">
          {{ showDetailForm.goods_price }}
        </el-form-item>
        <br>
        <el-form-item label="商品sku编号" :label-width="formLabelWidth" style="display:inline-block">
          {{ showDetailForm.sku_no }}
        </el-form-item>
        <br>
        <el-form-item label="商品规格" :label-width="formLabelWidth" style="display:inline-block">
          <el-tag
            v-for="item in showDetailForm.specification"
            :key="item.id"
            type="info"
            style="margin: 0px 2px"
          >{{ item.name }}: {{ item.value }}</el-tag>

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showDetailFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="showDetailFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
    <el-container>
      <!-- 搜索区 -->
      <el-header>
        <el-input v-model.number="search.order_no" placeholder="订单编号" style="width:300px;" />
        <el-button type="primary" icon="el-icon-search" @click="searchOrder()">搜索</el-button>
      </el-header>

      <!-- 功能区 -->
      <el-main style="height:600px;">
        <!-- 表格 组件 -->
        <datatable
          :table-fomat="tableFomat"
          :table-data="tableData"
          :total="tableData.length"
          :operate-visible="false"
        >
          <el-table-column label="收货人" width="200">
            <template slot-scope="scope">
              <el-tag
                type="info"
                style="margin: 0px 2px"
              >{{ scope.row.consignee_info.consignee }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="手机号" width="200">
            <template slot-scope="scope">
              <el-tag
                type="info"
                style="margin: 0px 2px"
              >{{ scope.row.consignee_info.phone }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="收获地址" width="200">
            <template slot-scope="scope">
              <el-tag
                type="info"
                style="margin: 0px 2px"
              >{{ scope.row.consignee_info.province_city_county + scope.row.consignee_info.detail_address }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="配送方式" width="200">
            <template slot-scope="scope">
              <el-tag
                type="info"
                style="margin: 0px 2px"
              >{{ deliveryType(scope.row.delivery_type) }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="订单状态" width="200">
            <template slot-scope="scope">
              <el-tag
                type="info"
                style="margin: 0px 2px"
              >{{ orderStatus(scope.row.order_status) }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="支付状态" width="200">
            <template slot-scope="scope">
              <el-tag
                type="info"
                style="margin: 0px 2px"
              >{{ payStatus(scope.row.pay_status) }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="支付类型" width="200">
            <template slot-scope="scope">
              <el-tag
                type="info"
                style="margin: 0px 2px"
              >{{ payType(scope.row.pay_type) }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" fixed="right">
            <template slot-scope="scope">
              <el-tooltip class="item" effect="dark" content="查看详情" placement="top-start">
                <el-button
                  type="primary"
                  size="mini"
                  @click="showDetail(scope.$index, scope.row)"
                >查看详情</el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </datatable>
      </el-main>
    </el-container>
  </el-container>
</template>
<style>
/* 容器头部 和  */
.el-header {
  line-height: 60px;
}
.el-aside {
  width: 200px !important;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  color: #333;
  background-color: white;
}
</style>
<script>
import datatable from '@/views/my-views/components/table'
import goods from '@/sql/table-field/index'
import { tsFormatTime } from '@/tools/util'
import { mapGetters } from 'vuex'
import orderConfig from '@/config/order'
export default {
  name: 'OrderWaitSend',
  components: {
    datatable
  },
  data() {
    return {
      // 解决notice通知栏覆盖问题
      notifyPromise: Promise.resolve(),
      // 搜索输入框的值
      search: {
        order_no: '',
        order_status: 6
      },
      // 表格显示格式(列名以及列对应显示的数据)
      tableFomat: goods.order,
      // 表格数据
      tableData: [],
      showDetailFormVisible: false,
      showDetailForm: {
        // 商品描述
        goods_brief: '',
        // 商品数量
        goods_num: 0,
        // 商品价格
        goods_price: 0,
        // 商品sku编号
        sku_no: '',
        // 商品规格
        specification: []

      },
      formLabelWidth: '120px'
    }
  },
  computed: {
    // 从store中的getters中获取数据(store/getters.js)
    ...mapGetters(['cancelOrders'])
  },
  mounted() {
    // 如果state中有数据则从state中读取
    if (this.cancelOrders.length !== 0) {
      // 使用缓存
      this.tableData = this.cancelOrders
    } else {
      // 获取商品列表
      this.getOrderList()
    }
  },

  methods: {
    // 配送方式转换前端显示格式
    deliveryType(data) {
      return orderConfig.deliveryType[data]
    },
    // 订单状态转换为前端显示格式
    orderStatus(data) {
      return orderConfig.status[data]
    },
    // 支付状态转换成为前端显示格式
    payStatus(data) {
      return orderConfig.payStatus[data]
    },
    // 支付类型转换成为前端显示格式
    payType(data) {
      return orderConfig.payType[data]
    },
    // tip提示方法
    tipformat(res, type, type_name) {
      if (res.errcode !== 0) {
        this.notifyPromise = this.notifyPromise
          .then(this.$nextTick)
          .then(() => {
            this.$notify.error({
              title: `${type}订单失败`,
              message: `错误代码:${res.errcode} 错误消息:${res.errmsg}`,
              duration: 0
            })
          })
      } else {
        this.notifyPromise = this.notifyPromise
          .then(this.$nextTick)
          .then(() => {
            this.$notify({
              title: `${type}订单成功`,
              message: `${type_name}--订单${type}成功`,
              type: 'success',
              duration: 6000
            })
          })
        // 重新从数据库获取数据
        this.getOrderList()
      }
    },
    // 数据库数据转换为前端显示数据格式
    dataBaseToFormData(value) {
      value.order_time = tsFormatTime(value.order_time, 'Y-M-D h:m:s')
      value.pay_time = tsFormatTime(value.pay_time, 'Y-M-D h:m:s')
      return value
    },
    // 错误请求消息提示规范
    catchError(type, err) {
      this.$notify.error({
        title: `${type}订单请求失败`,
        message: err,
        duration: 0
      })
    },
    searchOrder() {
      const type = '搜索'
      this.$store
        .dispatch('order/searchOrder', this.search)
        .then(res => {
          if (res.errcode !== 0) {
            if (res.errcode === 42001) {
              this.$notify.error({
                title: 'Access_token失效,请重新刷登录获取',
                message: `错误代码:${res.errcode} 错误消息:${res.errmsg}`,
                duration: 0
              })
            } else {
              this.$notify.error({
                title: '获取商品订单列表失败',
                message: `错误代码:${res.errcode} 错误消息:${res.errmsg}`,
                duration: 0
              })
            }
          } else {
            this.tableData = JSON.parse(res.resp_data).data.list
            this.tableData.map(res => {
              this.dataBaseToFormData(res)
            })
          }
        })
        .catch(err => {
          this.catchError(type, err)
        })
    },
    // 获取商品列表的方法
    getOrderList() {
      const type = '获取'
      this.$store
        .dispatch('order/getOrderListByOrderStatus', 6)
        .then(res => {
          if (res.errcode !== 0) {
            if (res.errcode === 42001) {
              this.$notify.error({
                title: 'Access_token失效,请重新刷登录获取',
                message: `错误代码:${res.errcode} 错误消息:${res.errmsg}`,
                duration: 0
              })
            } else {
              this.$notify.error({
                title: '获取商品订单列表失败',
                message: `错误代码:${res.errcode} 错误消息:${res.errmsg}`,
                duration: 0
              })
            }
          } else {
            this.tableData = JSON.parse(res.resp_data).data.list
            this.tableData.map(res => {
              this.dataBaseToFormData(res)
            })
            this.$store.commit('order/SET_WAITCANCELORDERS', this.tableData)
          }
        })
        .catch(err => {
          this.catchError(type, err)
        })
    },
    // 发货
    sendGoods(index, row) {
      const type = '发货'
      const formData = {
        _id: row._id,
        order_status: 2
      }
      this.$store
        .dispatch('order/updateOrderStatus', formData)
        .then(res => {
          console.log(res)
          this.tipformat(res, type, row._id)
        })
        .catch(err => {
          this.catchError(type, err)
        })
    },
    showDetail(index, row) {
      this.showDetailFormVisible = true
      this.showDetailForm.goods_brief = row.order_detail_list[0].goods_brief
      this.showDetailForm.goods_num = row.order_detail_list[0].goods_num
      this.showDetailForm.goods_price = row.order_detail_list[0].goods_price
      this.showDetailForm.sku_no = row.order_detail_list[0].sku_no
      this.showDetailForm.specification = row.order_detail_list[0].specification
    }
  }
}
</script>
